package de.jare.photoswapper;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings.System;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class photoswapper extends Activity {
     
    /* Constructor of this action */
    /* please keep this on top :) */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button bt1 = (Button)findViewById(R.id.take_imagebt);
        bt1.setOnClickListener(getImgListener);
        
        Button bt2 = (Button)findViewById(R.id.receivePicture);
        bt2.setOnClickListener(receivePictureListener);
        
        TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        
        // assign android id to HttpHandler
        HttpHandler.AndroidID = System.getString(this.getContentResolver(), System.ANDROID_ID);
        
		if (HttpHandler.AndroidID == null)
		{
			HttpHandler.AndroidID = "EmptyAndroidID";
		}
		
		HttpHandler.DeviceID = tm.getDeviceId();
        
		if (HttpHandler.DeviceID == null)
		{
			HttpHandler.DeviceID = "EmptyDeviceID";
		}
		
		HttpHandler.MobileNr = tm.getLine1Number();
        
		if (HttpHandler.MobileNr == null)
		{
			HttpHandler.MobileNr = "123456789";
		}
		
		TextView tv = (TextView)findViewById(R.id.androidid);
        tv.setText(HttpHandler.AndroidID);
        
        TextView tv2 = (TextView)findViewById(R.id.gsm);
        tv2.setText(HttpHandler.MobileNr);
        
        TextView tv3 = (TextView)findViewById(R.id.imei);
        tv3.setText(HttpHandler.DeviceID);
        
    }
    
    /* creating our menu */
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }

    /* event handler for menu items */
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case R.id.quit:
            Quit();
            return true;
        case R.id.about:
        	startAboutActivity();	
        	break;
        case R.id.register:
        	startRegisterActivity();
        	break;
        case R.id.get:
    		startGetActivity();
    		break;
        }
        return false;
    }
    
    View.OnClickListener getImgListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        	startPreviewActivity();
        }
    };
    
    View.OnClickListener receivePictureListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        	HttpHandler h = new HttpHandler();
        	h.ReceivePicture();
        }
    };
    
	public void startAboutActivity() {
		Intent i = new Intent(this, About.class);
		startActivity(i);
    }
    
    public void startPreviewActivity() {
    	Intent i = new Intent(this, Preview.class);
		startActivity(i);
    }
    
    public void startRegisterActivity() {
    	Intent i = new Intent(this, Register.class);
    	startActivity(i);
    }
    
    public void startGetActivity() {
    	Intent i = new Intent(this, Get.class);
        startActivity(i);
    }
    
    public void Quit() {
    	finish();
    }
    
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return super.onKeyDown(keyCode, event);
        }
        return false;
    }
}